<template>
  <view v-if="goods_info.goods_name" class="goods_detail_box_">
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(item,index) in goods_info.pics" :key="index">
       
        <image  class="swiper-item" :src="item.pics_big" ></image>
      </swiper-item> 
     
    </swiper>
    <view class="goods-info-box">
      <view class="price">
        ￥{{goods_info.goods_price}}
      </view>
      <view class="goods-info-body">
        <view class="goods_name">
          {{goods_info.goods_name}}
        </view>
        <view class="favi">
          <uni-icons type="star" size="20" color="red">
          </uni-icons>
          <text>收藏</text>
        </view>
        
      </view>
      <view class="kuaid">
        <text>快递：免运费 --{{cart}}</text>
      </view>
      <rich-text :nodes="goods_info.goods_introduce"></rich-text>
      <view class="goods_nav">
          <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick" @buttonClick="buttonClick" />
      </view>
    </view>
  </view>
</template>

<script> 
  import { mapState,mapMutations,mapGetters } from 'vuex'
  export default {
    computed:{
      ...mapState('m_cart',['cart']),
      ...mapGetters('m_cart',['total'])
    },

    watch:{
      
      total:{
        handler(newVal){
          let result = this.options.find(item => item.text === '购物车');
          if(result){
            result.info = newVal
          }
        },
        immediate:true  
      }
     
    },
    data() {
      return {
        goods_info:{},
         options: [ {
        			icon: 'shop',
        			text: '店铺',
        			info: 0,
        			infoBackgroundColor:'#007aff',
        			infoColor:"red"
        		}, {
        			icon: 'cart',
        			text: '购物车',
        		
        		}],
        	    buttonGroup: [{
        	      text: '加入购物车',
        	      backgroundColor: '#ff0000',
        	      color: '#fff'
        	    },
        	    {
        	      text: '立即购买',
        	      backgroundColor: '#ffa200',
        	      color: '#fff'
        	    }
        	    ]
      }
    },
    methods: {
      ...mapMutations('m_cart',['addToCarts']),
     async getGoodsDetail(good_id){
       let {data:result} = await uni.$http.get('/api/public/v1/goods/detail?goods_id=' + good_id)
       console.log(result);
       if(result.meta.status != 200){
         uni.$showError();
          return;
       }
       result.message.goods_introduce = result.message.goods_introduce.replace(/<img /g, '<img style="display:block;')
       this.goods_info = result.message
     },
     onClick(e){
       console.log(e);
       if(e.content.text === '购物车'){
        uni.switchTab({
          url:'/pages/cart/cart'
        })
       }
     },
     buttonClick(e){
       console.log(this.goods_info);
       if(e.content.text === '加入购物车'){
         let goods = {
           goods_id:this.goods_info.goods_id,
           goods_name:this.goods_info.goods_name,
           goods_price:this.goods_info.goods_price,
           goods_count:1,
           goods_small_logo:this.goods_info.goods_small_logo,
           goods_state:true
         }
         this.addToCarts(goods)
       }
       
     }
    }
    ,
    onLoad(option){
      const good_id = option.goods_id
      this.getGoodsDetail(good_id)
    }
  }
</script>

<style lang="scss" scoped>
swiper{
  height:750rpx ;
  image {
    width: 100%;
    height: 100%;
  }
}
.goods-info-box{
  padding: 10px;
  .price{
    color: orange;
    font-size: 24px;
    font-weight: 700;
    margin-top: 10px;
    margin-bottom: 10px;
  }
  .goods-info-body{
    display: flex;
    justify-content: space-between;
    .goods_name{
      font-size: 13px;
      padding-right: 10px;
    }
    .favi{
      width: 120px;
      display: flex;
      font-size: 12px;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      border-left: 1px solid mediumaquamarine;
    }
    
    
  }
}
.kuaid{
  font-size: 12px;
  color: gray;
  margin: 10px;
  margin-left: 0px;
}
.goods_nav{
  position: fixed;
  left: 0px;
  bottom: 0px;  
  width: 100%;
}
.goods_detail_box_{
  padding-bottom: 50px;
}

</style>
